﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFont>
#include <QPalette>

// QProcess *po;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    po = new QProcess(this);

    tt = 0;

    ui->label_2->clear();
    ui->tt_label_text->clear();

    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::darkBlue);
    ui->tt_label_text->setPalette(pe);
    //ui->pushButton->setStyleSheet("background-color: rgb(0, 255,0)");
    //ui->pushButton->setStyleSheet("color: red");
    QFont bfont("Microsoft YaHei", 25, 75);
    ui->pushButton->setFont(bfont);
    //ui->pushButton->setStyleSheet("color:blue");

#if 1
   po->setProcessChannelMode(QProcess::MergedChannels);

   connect(po,SIGNAL(started()),this,SLOT(fwupdate_Started()));//NA
   connect(po,SIGNAL(finished(int)),this,SLOT(fwupdate_Finished(int)));

   connect(po, SIGNAL(readyReadStandardOutput()), this, SLOT(fwupdate_outlog()));
   connect(po, SIGNAL(error(QProcess::ProcessError)), this, SLOT(poError(QProcess::ProcessError)));//NA

   connect(po, SIGNAL (readyRead()), this, SLOT (po_readReady()));//NA
   connect(po, SIGNAL (stateChanged(QProcess::ProcessState)), this, SLOT (po_stateChanged(QProcess::ProcessState)));//NA
 #endif
}

MainWindow::~MainWindow()
{
    delete ui;
    delete po;
}

void MainWindow::on_pushButton_clicked()
{

   static int it = 1;


#if 1
    
    if(po->state() == QProcess::NotRunning)
    {
        tt++;
        ui->tt_label_text->setText(QString::number(tt, 10));

        ui->textEdit->clear();
        QPalette pe;
        pe.setColor(QPalette::WindowText, Qt::red);
        ui->label_2->setPalette(pe);

        ui->label_2->setText("running...");
        //ui->label_2->setStyleSheet("color:red");
        QFont font("Microsoft YaHei", 25, 75);
        ui->label_2->setFont(font);
        qDebug()<< "it is: " << it;
        if(it == 1)
        {
            po->start("sudo /home/hexiongjun/dongle_linux_ifly/example/fwupdate  /home/hexiongjun/dongle_linux_ifly/example/8267_master_kma_dongle_v_15.15.bin 8");
            it = 2;
        } else if(it == 2){
            po->start("sudo /home/hexiongjun/dongle_linux_ifly/example/fwupdate  /home/hexiongjun/dongle_linux_ifly/example/8267_master_kma_dongle_v_1.4.bin 8");
            it = 1;
        }


    }

#endif
}

void MainWindow::fwupdate_outlog()
{
    QString text = po->readAllStandardOutput();
    qDebug() << "enter log output: " << text;
    //textEdit->setText(abc);
    ui->textEdit->append(text);
    ui->textEdit->update();


    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::green);

    if(text.contains("now reboot device"))
    {
        ui->label_2->clear();
        ui->label_2->setPalette(pe);
        ui->label_2->setText("PASS!");
        ui->label_2->setStyleSheet("color:green");
        QFont font("Microsoft YaHei", 30, 75);
        ui->label_2->setFont(font);
    } else {
        ui->label_2->clear();
        pe.setColor(QPalette::WindowText, Qt::red);
        ui->label_2->setPalette(pe);
        ui->label_2->setText("FAILED!");
       // ui->label_2->setStyleSheet("color:red");
        QFont font("Microsoft YaHei", 30, 75);
        ui->label_2->setFont(font);

    }
}

void MainWindow::fwupdate_Started()
{
    qDebug() << "fwupdate process started!";
}

void MainWindow::fwupdate_Finished(int ret)
{
   qDebug() << "fwupdate process finished!: " << ret;
}

void MainWindow::poError(QProcess::ProcessError err)
{
    qDebug() << "error have occured:" << err;
}

void MainWindow::po_readReady()
{
    qDebug() << "fwupdate process is readReady";
}
void MainWindow::po_stateChanged(QProcess::ProcessState s)
{
    qDebug()<< "fwupdate process state changed to : " << s;
}

